package com.hsnn.medstgmini.pro.project.controller;

import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.service.StdCompinfoManager;
import com.hsnn.medstgmini.pro.project.model.BidContract;
import com.hsnn.medstgmini.pro.project.model.BidContractParty;
import com.hsnn.medstgmini.pro.project.model.BidContractProd;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.model.ContractModel;
import com.hsnn.medstgmini.pro.project.model.form.BidContractForm;
import com.hsnn.medstgmini.pro.project.service.BidContractManager;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.pro.project.service.ContractModelManager;
import com.hsnn.medstgmini.sys.controller.SysNoticeController;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysNoticeManager;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/contract")
public class BidContractController {
	
	@Autowired
	private BidContractManager bidContractManager;
	
	@Autowired
	private ContractModelManager contractModelManager;
	
	@Autowired
	private StdCompinfoManager stdCompinfoManager;
	
	@Autowired
	private BidProjectManager bidProjectManager;
	
	@Autowired
	private HttpServletRequest request ;
	
	@Autowired
	private SysNoticeManager sysNoticeManager;
	
	@Value("#{sysProp['system.center.name']}")
	private String centerName;
	
	private static final Logger log = Logger
			.getLogger(SysNoticeController.class);
	
	/**
	 * @category 跳转到合同列表
	 * @author zhouqi
	 * @date 2015年9月10日10:13:18
	 * @param model
	 * @return String
	 */
	@RequestMapping("toBidContractList")
	public String toBidContractList(Model model){
		List<BidProject> projectList = sysNoticeManager.getProjectList();
		model.addAttribute("projectList", projectList);
		return "/pro/project/bidContractList";
	}
	
	/**
	 * @category 跳转到合同详情页面
	 * @author zhouqi
	 * @date 2015年9月23日10:48:07
	 * @param model
	 * @param contractId
	 * @return void
	 */
	@RequestMapping("toBidContractDetail")
	public String toContractDetail(Model model, String contractId){
		BidContract contract = bidContractManager.getBidContractById(contractId);
		List<BidContractParty> contractParty = bidContractManager.getPartyAByContractId(contractId, 0);//获取合同乙方列表
		List<BidContractParty> contractPartyAs = bidContractManager.getPartyAByContractId(contractId, 1);//获取合同甲方列表
		List<String> contractPartyBNames = new ArrayList<String>();
		for(int i =0; i< contractParty.size();i++){
			String partyId = contractParty.get(i).getContractParty();
			contractPartyBNames.add(stdCompinfoManager.queryStdCompInfoByCompId(partyId).getCompName());
		}
		List<BidContractProd> contractProds = bidContractManager.getProdByContractId(contractId);
		List<Map<String, String>> drugList = (List<Map<String, String>>) bidContractManager.getDrugInfoByContractId(contractId);
		BidProject bidproject = bidProjectManager.getBidProjectById(contract.getProjId());
		String updDatetime = DateUtil.dateTime(contract.getUpdDatetime());
		String auditDatetime = DateUtil.dateTime(contract.getAuditDatetime());
		String auditStatus="";
		if(null != contract.getAuditStatus()){
			switch(contract.getAuditStatus()){//0新建 1提交 2 审核通过 3 审核不通过
				case 0: auditStatus="新建";break;
				case 1: auditStatus="提交";break;
				case 2: auditStatus="审核通过";break;
				case 3: auditStatus="审核不通过";break;
				default:auditStatus="";
			}
		}
	    model.addAttribute("contract", contract);
	    if(null != bidproject){
	    	 model.addAttribute("projName", bidproject.getName());
	    }
	    model.addAttribute("contractPartyBNames", contractPartyBNames);
	    model.addAttribute("contractProds", contractProds);
	    model.addAttribute("updDatetime", updDatetime);
	    model.addAttribute("auditDatetime", auditDatetime);
	    model.addAttribute("auditStatus", auditStatus);
	    model.addAttribute("drugList",drugList);//合同药品
	    if(contractPartyAs.size() > 0){
	    	String contractPartyAId = contractPartyAs.get(0).getContractParty();
	    	if(StringUtils.isBlank(contractPartyAId)){
	    		model.addAttribute("contractPartyA", centerName);
	    	}else{
	    		String contractPartyAName = stdCompinfoManager.queryStdCompInfoByCompId(contractPartyAId).getCompName();
	    		model.addAttribute("contractPartyA", contractPartyAName);
	    	}
	    }else{
	    	model.addAttribute("contractPartyA", centerName);
	    }
		return "/pro/project/bidContractDetail";
	}
	
	/**
	 * @category 跳转到新增页面
	 * @author zhouqi
	 * @date 2015年9月10日10:15:29
	 * @param model
	 * @param contractId
	 * @return
	 */
	@RequestMapping("toBidContractEdit")
	public String toBidContractEdit(Model model, String contractId,String contractModelId){
		String optType = request.getParameter("opType");
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String defaultPartyA = user.getName();
		if(StringUtils.isNotBlank(contractId)){
			BidContract contract = bidContractManager.getBidContractById(contractId);
			model.addAttribute("contract",contract);
		}
		if(StringUtils.isNotBlank(contractModelId)){
			ContractModel contractModel = contractModelManager.getContractModelById(contractModelId);          
			model.addAttribute("contractModel",contractModel);
		}
		if(StringUtils.isNotBlank(optType)){        
			model.addAttribute("optType",optType);
		}
		
		List<BidProject> projectList = sysNoticeManager.getProjectList();
		model.addAttribute("projectList", projectList);
		model.addAttribute("defaultPartyA", defaultPartyA);
		return "/pro/project/bidContractEdit";
	}
	/**
	 * @category 跳转到编辑页面
	 * @author zhouqi
	 * @date 2015年9月10日10:15:29
	 * @param model
	 * @param contractId
	 * @return
	 */
	@RequestMapping("toEditContract")
	public String toEditContract(Model model, String contractId){
		BidContract contract = bidContractManager.getBidContractById(contractId);//合同
		BidContractParty partyA = bidContractManager.getPartyAByContractId(contractId, 1).get(0);//甲方
		BidContractParty partyB = bidContractManager.getPartyAByContractId(contractId, 0).get(0);//乙方
		String partyAName = "";
		String partyBName ="";
		if( StringUtils.isNotBlank(partyA.getContractParty())){
			partyAName = stdCompinfoManager.queryStdCompInfoByCompId(partyA.getContractParty()).getCompName();
		}else{
			partyAName= centerName;
		}
		if( StringUtils.isNotBlank(partyB.getContractParty())){
			partyBName = stdCompinfoManager.queryStdCompInfoByCompId(partyB.getContractParty()).getCompName();
		}else{
			partyBName= centerName;
		}
		Blob blobContent = contract.getBlobContractContent();
		if(null!=blobContent){
			try {
				String content = new String(blobContent.getBytes((long)1, (int)blobContent.length()),"utf-8");
				content = content.replaceAll("\"", "'");//主要为了解决前台多加引号问
				content = content.replaceAll("\\n", "").replace("\\r", "");
				model.addAttribute("content", content);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		String jsonDrugInfo = JSON.toJSONString( bidContractManager.getDrugInfoByContractId(contractId));
	    model.addAttribute("contract", contract);
	    model.addAttribute("partyA", partyA);
	    model.addAttribute("partyB", partyB);
	    model.addAttribute("partyAName", partyAName);
	    model.addAttribute("partyBName", partyBName);
	    model.addAttribute("jsonDrugInfo",jsonDrugInfo);
		return "/pro/project/editContract";
	}
	/**
	 * @category 保存编辑的合同
	 * @author zhouqi
	 * @date 2015年9月23日17:00:54
	 * @param contractId
	 */
	@RequestMapping("saveEditContract")
	@ResponseBody
	public Pagination saveEditContract(String contractId,String partyAndDrugData){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		try{
			String partyAId = request.getParameter("partyAId");
			String partyBId = request.getParameter("partyBId");
			String content = request.getParameter("content");
			String contractTitle = request.getParameter("contractTitle");
			Map<String, Object> map = (Map<String, Object>)JSON.parse(partyAndDrugData);
			String DrugIds = (String) map.get("drugIds");
			String[] drugIds = DrugIds.split(",");//获取新的药品信息
			if(StringUtils.isNotBlank(contractId)){
				BidContract contract = bidContractManager.getBidContractById(contractId);
				if(null!=contract){//修改合同
					contract.setContractTitle(contractTitle);
					contract.setUpdDatetime(DateUtil.getTimeStamp());
					contract.setUpdUser(user.getName());
					contract.setContent(content);
					bidContractManager.genePdfAndSwf(contract, request);
					bidContractManager.saveOrUpdateContract(contract);
					bidContractManager.writeLogOfContract(contract);
				}
				//保存合同甲方乙方
				BidContractParty bidContractPartyA = bidContractManager.getPartyAByContractId(contractId, 1).get(0);//获取甲方
				BidContractParty bidContractPartyB = bidContractManager.getPartyAByContractId(contractId, 0).get(0);//获取乙方
				bidContractPartyA.setContractParty(partyAId);
				bidContractPartyB.setContractParty(partyBId);
				bidContractManager.saveBidContractParty(bidContractPartyA);
				bidContractManager.saveBidContractParty(bidContractPartyB);
				bidContractManager.writeLogOfContractParty(bidContractPartyA);//写日志
				bidContractManager.writeLogOfContractParty(bidContractPartyB);//写日志
				//保存药品
				List<String> newDrugs = Arrays.asList(drugIds);//新药品列表
				List<BidContractProd> oldDrugs = bidContractManager.getProdByContractId(contractId);//旧药品列表
				for(int i = 0; i< oldDrugs.size(); i++){
					oldDrugs.get(i).setStatus(0);
				}
				for(int i = 0; i < newDrugs.size(); i++){
					String newDrug = newDrugs.get(i);
					for(int j = 0 ; j< oldDrugs.size(); j++){
						if(newDrug.equals(oldDrugs.get(j).getCompProdId())){//药品已存在
							oldDrugs.get(j).setStatus(1);
							break;
						}
						if(j==oldDrugs.size()-1){//新增一个药品
							BidContractProd bidContractProd = new BidContractProd();
							bidContractProd.setId(UUID.randomUUID().toString());
							bidContractProd.setContractId(contractId);
							bidContractProd.setCompProdId(newDrug);
							bidContractProd.setStatus(1);
							bidContractProd.setUpdDatetime(DateUtil.getTimeStamp());
							bidContractProd.setUpdUser(user.getName());
							oldDrugs.add(bidContractProd);
							
						}
					}
					if(oldDrugs.size()==0){
						BidContractProd bidContractProd = new BidContractProd();
						bidContractProd.setId(UUID.randomUUID().toString());
						bidContractProd.setContractId(contractId);
						bidContractProd.setCompProdId(newDrug);
						bidContractProd.setStatus(1);
						bidContractProd.setUpdDatetime(DateUtil.getTimeStamp());
						bidContractProd.setUpdUser(user.getName());
						oldDrugs.add(bidContractProd);
					}
				}
				for(int i = 0; i< oldDrugs.size();i++){
				    bidContractManager.saveBidContractProd(oldDrugs.get(i));
				    bidContractManager.writeLogOfContractDrug(oldDrugs.get(i));//写日志
				}
					
			}
			page.setSuccess(true);
			page.setMsg("保存成功");
		}catch(Exception e){
			page.setSuccess(false);
			page.setMsg("保存失败");
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转到合同日志列表
	 * @author zhouqi
	 * @date 2015年9月10日10:15:06
	 * @param model
	 * @param contractId
	 * @return
	 */
	@RequestMapping("toBidContractLogList")
	public String toBidContractLogList(Model model, String contractId){
		if(StringUtils.isNoneBlank(contractId)){
			model.addAttribute("contractId", contractId);
		}
		return "/pro/project/bidContractLogList";
	}
	
	/**
	 * @category 跳转到合同日志详情
	 * @author zhouqi
	 * @date 2015年9月10日10:29:37
	 * @param contractId
	 * @param updDateTime
	 * @return
	 */
	@RequestMapping("toContractLogDetail")
	public String toContractLogDetail(Model model, String contractId, String updDateTime){
		List<Map<String, Object>> list = bidContractManager.getCompareLog(contractId, updDateTime);
		List<String> bidContractLogList = bidContractManager.getCompareContractLog(contractId, updDateTime);
		Map<String,Object> oldContract = new HashMap<String,Object>();
		Map<String,Object> newContract = new HashMap<String,Object>();
		if(list.size()>1){
			oldContract = list.get(1);
			newContract = list.get(0);
		}
		if(list.size() == 1){
			oldContract = list.get(0);
			newContract = list.get(0);
		}
		if(bidContractLogList.size() > 1){
			String oldContent = bidContractLogList.get(1);
			oldContract.put("content", oldContent);
			String newContent = bidContractLogList.get(0);
			newContract.put("content", newContent);
		}
		if(bidContractLogList.size() == 1){
			String oldContent = bidContractLogList.get(1);
			oldContract.put("content", oldContent);
			String newContent = bidContractLogList.get(0);
			newContract.put("content", newContent);
		}
		model.addAttribute("oldContract", oldContract);
		model.addAttribute("newContract", newContract);
		return "/pro/project/bidContractLogDetail";
	}
	/**
	 * @category 跳转到合同审核页面
	 * @author zhouqi
	 * @date 2015年9月22日21:46:36
	 * @param model
	 * @return
	 */
	@RequestMapping("toBidContractAudit")
	public String toBidContractAudit(Model model){
		List<BidProject> projectList = sysNoticeManager.getProjectList();
		model.addAttribute("projectList", projectList);
		return "/pro/project/contractAudit";
	}
	/**
	 * @category 跳转到审核详情页面
	 * @author zhouqi
	 * @date 2015年9月22日22:30:20
	 * @param contractId
	 * @param model
	 * @param contractId
	 * @return String
	 */
	@RequestMapping("toContractAuditDetail")
	public String toContractAuditDetail(Model model, String contractId){
		BidContract contract = bidContractManager.getBidContractById(contractId);
		List<BidContractParty> contractParty = bidContractManager.getPartyAByContractId(contractId, 0);//获取合同乙方列表
		List<BidContractParty> contractPartyAs = bidContractManager.getPartyAByContractId(contractId, 1);//获取合同甲方列表
		List<String> contractPartyBNames = new ArrayList<String>();
		for(int i =0; i< contractParty.size();i++){
			String partyId = contractParty.get(i).getContractParty();
			contractPartyBNames.add(stdCompinfoManager.queryStdCompInfoByCompId(partyId).getCompName());
		}
		List<BidContractProd> contractProds = bidContractManager.getProdByContractId(contractId);
		BidProject bidproject = bidProjectManager.getBidProjectById(contract.getProjId());
	    model.addAttribute("contract", contract);
	    if(null != bidproject){
	    	 model.addAttribute("projName", bidproject.getName());
	    }
	    model.addAttribute("contractPartyBNames", contractPartyBNames);
	    model.addAttribute("contractProds", contractProds);
	    model.addAttribute("contractId", contractId);
	    if(contractPartyAs.size() > 0){
	    	String contractPartyAId = contractPartyAs.get(0).getContractParty();
	    	if(StringUtils.isBlank(contractPartyAId)){
	    		model.addAttribute("contractPartyA", centerName);
	    	}else{
	    		String contractPartyAName = stdCompinfoManager.queryStdCompInfoByCompId(contractPartyAId).getCompName();
	    		model.addAttribute("contractPartyA", contractPartyAName);
	    	}
	    }else{
	    	model.addAttribute("contractPartyA", centerName);
	    }
		return "/pro/project/contractAuditDetail";
	}
	
	/**
	 * @category 弹出合同乙方选择框
	 * @author zhouqi
	 * @date 2015年9月18日10:26:21
	 * @return
	 */
	@RequestMapping("toSelectContractParty")
	public String toSelectContractParty(Model model,String strPartyId,String projId){
		model.addAttribute("strPartyId",strPartyId);
		model.addAttribute("projId",projId);
		return "/pro/project/bidContractProduceComp";
	}
	
	/**
	 * @category 弹出合药品选择框
	 * @author zhouqi
	 * @date 2015年9月18日10:26:21
	 * @return
	 */
	@RequestMapping("toSelectContractDrug")
	public String toSelectDrug(Model model, String strDrugId, String compId){
		model.addAttribute("strDrugId",strDrugId);
		model.addAttribute("prodPompId",compId);
		return "/pro/project/bidContractDrug";
	}
	
	/**
	 * @category 获取合同列表
	 * @author zhouqi
	 * @date 2015年9月10日10:16:07
	 * @param form
	 * @return
	 */
	@RequestMapping("getContractList")
	@ResponseBody
	public Pagination getContractList(BidContractForm form){
		Pagination page = new Pagination(request);
		String auditStatus = request.getParameter("auditStatus");
		Map<String, String> conditions = page.getConditions();
		conditions.put("projId",form.getProjId());
		conditions.put("contractTitle", form.getContractTitle());
		conditions.put("auditStatus", form.getAuditStatus());
		conditions.put("status", form.getStatus());
		conditions.put("auditStatus",auditStatus);
		conditions.put("isPartyA","0");
		try{
			page = bidContractManager.getContractListPage(page);
			List<Map<String, String>>  list= (List<Map<String, String>>) page.getRows();
			for(int i = 0; i< list.size(); i++){
				Map<String, String> params = list.get(i);
				if(null== params.get("contractPartyA")){
					params.put("contractPartyA", centerName);
				}
			}
			page.setSuccess(true);
		}catch(Exception e){
			page.setSuccess(false);
			log.error("",e);
		}
		return page;
	}
	/**
	 * @category 获取合同日志列表
	 * @author zhouqi
	 * @date 2015年9月10日10:16:47
	 * @param startPoint
	 * @param endPoint
	 * @return
	 */
	@RequestMapping("getContactLogList")
	@ResponseBody
	public Pagination getContactLogList(String startPoint, String endPoint){
		Pagination page = new Pagination(request);
		String contractId = request.getParameter("contractId");
		Map<String, String> conditions = page.getConditions();
		if(StringUtils.isNotBlank(startPoint)){
			conditions.put("startPoint", startPoint);
		}
		
		if(StringUtils.isNotBlank(endPoint)){
			conditions.put("endPoint", endPoint);
		}
		
		if(StringUtils.isNotBlank("contractId")){
			conditions.put("contractId", contractId);
		}
		try{
			bidContractManager.getContractLogListPage(page);
			page.setSuccess(true);
		}catch(Exception e){
			page.setSuccess(true);
			log.error("",e);
		}
		return page;
	}

	/**
	 * @category 生成或更新合同
	 * @author zhouqi
	 * @date 2015年9月18日14:46:51
	 * @param startPoint
	 * @param endPoint
	 * @return
	 */
	@RequestMapping("saveOrUpdateContract")
	@ResponseBody
	public Pagination saveOrUpdateContract(String partyAndDrugData, BidContract contract){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String partyAId = request.getParameter("partyAId");
		JSONArray jsonArray = JSON.parseArray(partyAndDrugData);
		try{
			
			for(int i = 0; i< jsonArray.size(); i++){
				BidContract singContract = new BidContract();
				Map<String, Object> map = (Map<String, Object>) jsonArray.get(i);

				//设置contract的值 新增
				singContract.setContractId(UUID.randomUUID().toString());
				singContract.setProjId(contract.getProjId());
				singContract.setContractTitle(contract.getContractTitle());
				singContract.setContent(contract.getContent());
				singContract.setContractModelId(contract.getContractModelId());
				singContract.setAuditStatus(0);
				singContract.setUpdDatetime(DateUtil.getTimeStamp());
				singContract.setUpdUser(user.getName());
				singContract.setContractCode(bidContractManager.getContractCode());
				singContract.setStatus(1);
				bidContractManager.genePdfAndSwf(singContract,request);
				bidContractManager.saveOrUpdateContract(singContract);
				bidContractManager.writeLogOfContract(singContract);//写入日志
				//设置甲方乙方
				BidContractParty contractPartyB = new BidContractParty();
				BidContractParty contractPartyA = new BidContractParty();
				contractPartyA.setContractId(singContract.getContractId());
				contractPartyA.setId(UUID.randomUUID().toString());
				contractPartyA.setIsPartyA(1);
				contractPartyA.setContractParty(partyAId);
				contractPartyA.setStatus(1);
				contractPartyA.setUpdDatetime(DateUtil.getTimeStamp());
				contractPartyA.setUpdUser(user.getName());
				contractPartyB.setContractId(singContract.getContractId());
				contractPartyB.setContractParty(map.get("party").toString());
				contractPartyB.setId(UUID.randomUUID().toString());
				contractPartyB.setIsPartyA(0);
				contractPartyB.setStatus(1);
				contractPartyB.setUpdDatetime(DateUtil.getTimeStamp());
				contractPartyB.setUpdUser(user.getName());
				bidContractManager.saveBidContractParty(contractPartyB);
				bidContractManager.saveBidContractParty(contractPartyA);
				bidContractManager.writeLogOfContractParty(contractPartyA);
				bidContractManager.writeLogOfContractParty(contractPartyB);
	            //设置合同与药品的关联关系
				String[] drugsString = map.get("drugs").toString().split(",");
				for(int j = 0; j < drugsString.length;j++){
					if(StringUtils.isNotBlank(drugsString[j])){
						BidContractProd drug = new BidContractProd();
						drug.setId(UUID.randomUUID().toString());
						drug.setCompProdId(drugsString[j]);
						drug.setContractId(singContract.getContractId());
						drug.setStatus(1);
						drug.setUpdDatetime(DateUtil.getTimeStamp());
						drug.setUpdUser(user.getName());
						bidContractManager.saveBidContractProd(drug);
						bidContractManager.writeLogOfContractDrug(drug);
					}
				}
			}
			page.setSuccess(true);
			page.setMsg("保存成功!");
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("保存失败!");
		}
		return page;
	}
	/**
	 * @category 改变审核状态 0新建 1提交 2 审核通过 3 审核不通过
	 * @author zhouqi
	 * @date 2015年9月10日10:23:46
	 * @param overIds
	 * @param auditStatus
	 * @return
	 */
	@RequestMapping("changeAuditStatus")
	@ResponseBody
	public Pagination changeAuditStatus(String overId, String auditStatus){
		Pagination page = new Pagination();
		String[] ids = overId.split(",");
		try{
			for(int i = 0; i< ids.length; i++){
				BidContract contract = bidContractManager.getBidContractById(ids[i]);
				Integer oldStatus = contract.getAuditStatus();
				if((oldStatus == 1) ||( oldStatus == 2)){
					page.setSuccess(false);
					if(oldStatus==1){
						page.setMsg("该合同已经提交！请另选合同");
					}else{
						page.setMsg("合同已经审核成功！请另选合同");
					}
					return page;
				}
				contract.setAuditStatus(Integer.parseInt(auditStatus));
				bidContractManager.saveOrUpdateContract(contract);
				bidContractManager.writeLogOfContract(contract);
			}
			page.setSuccess(true);
			page.setMsg("保存成功");
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("保存失败");
		}
		return page;
	}
	
	/**
	 * @category 改变状态
	 * @author zhouqi
	 * @date 2015年9月10日10:24:48
	 * @param overIds
	 * @param status
	 * @return
	 */
	@RequestMapping("changeStatus")
	@ResponseBody
	public Pagination changeStatus(String overId, String status){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String[] ids = overId.split(",");
		try{
			for(int i = 0; i< ids.length; i++){
				BidContract contract = bidContractManager.getBidContractById(ids[i]);
				contract.setStatus(Integer.parseInt(status));
				contract.setUpdDatetime(DateUtil.getTimeStamp());
				contract.setUpdUser(user.getName());
				bidContractManager.saveOrUpdateContract(contract);
				bidContractManager.writeLogOfContract(contract);
			}
			page.setSuccess(true);
			page.setMsg("保存成功");
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("保存失败");
		}
		return page;
	}
	
	/**
	 * @category 合同审核
	 * @author zhouqi
	 * @date 2015年9月10日10:24:48
	 * @param overIds
	 * @param status
	 * @return
	 */
	@RequestMapping("contractAudit")
	@ResponseBody
	public Pagination contractAudit(String contractId, String auditStatus, String auditRemark){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		try{
			BidContract contract = bidContractManager.getBidContractById(contractId);
			contract.setAuditStatus(Integer.parseInt(auditStatus));//设置审核状态
			contract.setAuditDatetime(DateUtil.getTimeStamp());//设置审核时间
			contract.setAuditUser(user.getName());//设置审核人
			contract.setAuditRemark(auditRemark);//设置审核备注
			bidContractManager.saveOrUpdateContract(contract);
			page.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 獲取企業列表
	 * @author zhouqi
	 * @date 2015年9月10日10:24:48
	 * @param overIds
	 * @param status
	 * @return
	 */
	@RequestMapping("queryStdCompInfoList")
	@ResponseBody
	public Pagination queryStdCompinfoList(){
		Pagination page =new Pagination();
		String projId = request.getParameter("projId");
		String compName = request.getParameter("compName");
		String compCode = request.getParameter("compCode");
		if(StringUtils.isNotBlank("projId")){
			page.getConditions().put("projId", projId);
		}
		if(StringUtils.isNotBlank("compName")){
			page.getConditions().put("compName", compName);
		}
		if(StringUtils.isNotBlank("compCode")){
			page.getConditions().put("compCode",compCode);
		}
		try{
			page = bidContractManager.queryStdCompinfoList(page);
			page.setSuccess(true);
		}catch(Exception e){
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 獲取藥品列表
	 * @author zhouqi
	 * @date 2015年9月10日10:24:48
	 * @param overIds
	 * @param status
	 * @return
	 */
	@RequestMapping("queryStdDrugInfoList")
	@ResponseBody
	public Pagination queryStdDruginfoList(){
		Pagination page =new Pagination();
		String compId = request.getParameter("compId");
		if(StringUtils.isNotBlank("compId")){
			page.getConditions().put("compId", compId);
		}
		try{
			page.setSuccess(true);
		}catch(Exception e){
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
}
